﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ahi.Entity;
using Ahi.Controller;

namespace Ahi.Base.Kontroller
{
    public class TipTanimiLookUp : DevExpress.XtraEditors.LookUpEdit
    {
        List<TipTanimiEntity> tipListesi;
        TipTanimiEntity tipSec = new TipTanimiEntity { Isim = "[YENİ TİP TANIMLA]" };

        public virtual void Yenile<T>()
            where T : TipTanimiEntity, new()
        {
            tipListesi = MongoAdaptor.Instance.GetData<T>();
            tipListesi.Add(tipSec);
            this.SetDataSource("Isim", "Id", tipListesi);
            EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(repositoryItemLookUpBelgeTipi_EditValueChanging);
        }

        void repositoryItemLookUpBelgeTipi_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            if (!e.NewValue.Equals(tipSec)) return;

            string yeniTip = string.Empty;
            if (GirdiForm.ShowGirdiDialog<string>(ref yeniTip, "Lütfen tanımlayacağınız yeni belge tipinin ismini giriniz") == System.Windows.Forms.DialogResult.OK)
            {
                tipListesi.Add(new T { Isim = yeniTip });
                tipListesi.Remove(tipSec);
                MongoAdaptor.Instance.Save<T>(tipListesi);
                EditValueChanging -= new DevExpress.XtraEditors.Controls.ChangingEventHandler(repositoryItemLookUpBelgeTipi_EditValueChanging);
                Yenile();

                EditValue = tipListesi.Where(x => x.Isim == yeniTip).FirstOrDefault();
            }
            else
                e.Cancel = true;
        }
    }
}
